package com.mzba.zxdlyllp;

import java.util.LinkedHashMap;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.waps.AdView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.mzba.zxdlyllp.db.MasturbationTable.MasturbationEntity;
/**
 * 
 * @author 06peng
 *
 */
public class MasturbationSlidingSettingFragment extends SherlockFragment {

	private ListView listView;
	private MasturbationActivity mainActivity;
	private LinearLayout userLayout;
	
	private HeaderListViewAdapter listAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mainActivity = (MasturbationActivity) getSherlockActivity();
		listAdapter = new HeaderListViewAdapter();
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_sm_list, null);
		listView = (ListView) view.findViewById(R.id.list);
		userLayout = (LinearLayout) view.findViewById(R.id.AdLinearLayout);
		new AdView(mainActivity, userLayout).DisplayAd();
		return view;
	}
	

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		NavFragmentItemAdapter adapterIndex = new NavFragmentItemAdapter(mainActivity);
		adapterIndex.add(new NavFragmentItem("自慰介绍", 0));
		listAdapter.addSection("首页", adapterIndex);
		
		NavFragmentItemAdapter adapterMan = new NavFragmentItemAdapter(mainActivity);
		for (MasturbationEntity entity : mainActivity.list) {
			if (entity.getType() == 1) {
				adapterMan.add(new NavFragmentItem(entity.getTitle(), 0));
			}
		}
		listAdapter.addSection("男性自慰技巧", adapterMan);
		
		NavFragmentItemAdapter adapterWoman = new NavFragmentItemAdapter(mainActivity);
		for (MasturbationEntity entity : mainActivity.list) {
			if (entity.getType() == 2) {
				adapterWoman.add(new NavFragmentItem(entity.getTitle(), 0));
			}
		}
		listAdapter.addSection("女性自慰技巧", adapterWoman);
		
		listView.setAdapter(listAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					mainActivity.getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
			        NavFragmentItem item = (NavFragmentItem) listView.getAdapter().getItem(arg2);
			        if (item.tag.equals("自慰介绍")) {
			        	FragmentNavigation.replace(mainActivity, new MasturbationFragment(), R.id.content, "masturbationFragment");
			        } else {
			        	FragmentNavigation.replace(mainActivity, new MasturbationFragmentItem(item.tag), R.id.content, "masturbationFragmentItem");
			        }
			        mainActivity.getSlidingMenu().toggle();
		        	mainActivity.actionBar.setTitle(item.tag);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private class NavFragmentItem {
		public String tag;
		public int iconRes;
		public NavFragmentItem(String tag, int iconRes) {
			this.tag = tag; 
			this.iconRes = iconRes;
		}
	}

	public class NavFragmentItemAdapter extends ArrayAdapter<NavFragmentItem> {
		
		public NavFragmentItemAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_sm_list_item, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			icon.setImageResource(getItem(position).iconRes);
			icon.setVisibility(View.GONE);
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);
			return convertView;
		}
	}
	
	public class HeaderListViewAdapter extends BaseAdapter {
		
		public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
		public final ArrayAdapter<String> headers;
		public final static int TYPE_SECTION_HEADER = 0;
		
		public HeaderListViewAdapter() {
			headers = new ArrayAdapter<String>(mainActivity, R.layout.list_header_image, R.id.list_header_title);
		}
		
		public void addSection(String section, Adapter adapter) {
			this.headers.add(section);
			this.sections.put(section, adapter);
		}
		
		@Override
		public Object getItem(int position) {
			for (Object section : this.sections.keySet()) {
				Adapter adapter = sections.get(section);
				int size = adapter.getCount() + 1;

				if (position == 0) {
					return section;
				}
				if (position < size) {
					return adapter.getItem(position - 1);
				}
				// otherwise jump into next section
				position -= size;
			}
			return null;
		}

		@Override
		public int getCount() {
			// total together all sections, plus one for each section header
			int total = 0;
			for(Adapter adapter : this.sections.values()) {
				total += adapter.getCount() + 1;
			}
			return total;
		}

		public int getViewTypeCount() {
			// assume that headers count as one, then total all sections
			int total = 1;
			for(Adapter adapter : this.sections.values()) {
				total += adapter.getViewTypeCount();
			}
			return total;
		}

		public int getItemViewType(int position) {
			int type = 1;
			for (Object section : this.sections.keySet()) {
				Adapter adapter = sections.get(section);
				int size = adapter.getCount() + 1;

				// check if position inside this section
				if (position == 0) {
					return TYPE_SECTION_HEADER;
				}
				if (position < size) {
					return type + adapter.getItemViewType(position - 1);
				}
				// otherwise jump into next section
				position -= size;
				type += adapter.getViewTypeCount();
			}
			return -1;
		}

		public boolean areAllItemsSelectable() {
			return false;
		}

		public boolean isEnabled(int position) {
			return (getItemViewType(position) != TYPE_SECTION_HEADER);
		}


		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			int sectionnum = 0;
//			View mView = null;
//			LayoutInflater mInflater = LayoutInflater.from(mainActivity);
//			mView = mInflater.inflate(R.layout.list_spinner, parent, false);

			for (Object section : this.sections.keySet()) {
				Adapter adapter = sections.get(section);
				int size = adapter.getCount() + 1;

				// check if position inside this section
				if (position == 0) {
					return headers.getView(sectionnum, convertView, parent);
				}
				if (position < size) {
					return adapter.getView(position - 1, convertView, parent);
				}

				// otherwise jump into next section
				position -= size;
				sectionnum++;
			}
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		
	}

	
	@Override 
	public void onDestroy() {
		super.onDestroy();
	}
	
}
